import java.io.*;
import java.net.*;

public class KnockKnockClient{

    public static void main (String[] args) throws IOException{
    
        Socket kksocket = null;
        PrintWriter out = null; // to send to server
        BufferedReader in = null; //to get from server


                try{
            
            kksocket = new Socket("localhost",4444);//connect to server
            out = new PrintWriter(kksocket.getOutputStream(),true);
            in = new BufferedReader(
                    new InputStreamReader(kksocket.getInputStream()));

        }
       
        catch(UnknownHostException e){
            System.err.println("Error in connection to server 4444");
            System.exit(1);
        }
        catch(IOException e){
            
            System.err.println("Error in IO of connection");
            System.exit(1);
        }

        BufferedReader inLine = new BufferedReader(new InputStreamReader(System.in));
        String from_server = null;
        String from_user = null;
        
        while((from_server=in.readLine())!=null){ //wait answer from server
            
            System.out.println("Answer from server is :"+from_server);
            if (from_server.equals("See"))
                break;

            from_user = inLine.readLine();
            if (from_user!=null){
            
                System.out.println("Client says :"+from_user);
                out.println(from_user);
            }

        }

        out.close();
        inLine.close();
        in.close();
        kksocket.close();
    }    
}
